package com.atguigu.gulimall.coupon.service.impl;

import com.atguigu.common.to.MemberPrice;
import com.atguigu.common.to.SkuReductionTo;
import com.atguigu.gulimall.coupon.entity.MemberPriceEntity;
import com.atguigu.gulimall.coupon.entity.SkuLadderEntity;
import com.atguigu.gulimall.coupon.service.MemberPriceService;
import com.atguigu.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.coupon.dao.SkuFullReductionDao;
import com.atguigu.gulimall.coupon.entity.SkuFullReductionEntity;
import com.atguigu.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 1-90-1 商品服务-API-新增商品-调用远程服务保存优惠等信息
     * 保存sku的优惠信息
     */
    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        // sku的优惠，满减等信息 mall-coupon-》
        // sms_sku_ladder打折表，
        // sms_sku_full_reduction满减表，
        // sms_member_price会员价格

        //sms_sku_ladder打折表
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        //共几件
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        //打几折
        skuLadderEntity.setDiscount(skuLadderEntity.getDiscount());
        //是否叠加其他优惠
        skuLadderEntity.setAddOther(skuLadderEntity.getAddOther());
        // 折扣价可以在最后下订单再计算
        //skuLadderEntity.setPrice(skuReductionTo.get);
        // 1-92-1 商品服务-API-新增商品-商品保存其他问题处理
        if (skuReductionTo.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }

        //sms_sku_full_reduction满减表
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal(0)) > 0) {
            this.save(skuFullReductionEntity);
        }

        //sms_member_price会员价格
        List<MemberPrice> memberPrices = skuReductionTo.getMemberPrice();

        List<MemberPriceEntity> collect = memberPrices.stream().map((item) -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setAddOther(1);
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            return memberPriceEntity;
            // 1-92-1 商品服务-API-新增商品-商品保存其他问题处理  , 去除掉会员价格为0的不进行保存
        }).filter(item -> item.getMemberPrice().compareTo(new BigDecimal(0)) > 0)
                .collect(Collectors.toList());

        memberPriceService.saveBatch(collect);
    }

}